<!DOCTYPE html>

<html>

<head>
    <title>Example 03.04 - Directional Light</title>
    <script type="text/javascript" src="js/three.js"></script>

    <script type="text/javascript" src="js/libs/stats.min.js"></script>
    <script type="text/javascript" src="js/libs/dat.gui.js"></script>
    <style>
        body {
            /* set margin to 0 and overflow to hidden, to go fullscreen */
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>

<div id="Stats-output">
</div>
<!-- Div which will hold the Output -->
<div id="WebGL-output">
</div>

<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">

    // once everything is loaded, we run our Three.js stuff.
    function init() {

        var stats = initStats();

        // create a scene, that will hold all our elements such as objects, cameras and lights.
        var scene = new THREE.Scene();

        // create a camera, which defines where we're looking at.
        var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);

        // create a render and set the size
        var renderer = new THREE.WebGLRenderer();

        renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.shadowMapEnabled = true;

        // create the ground plane
        var planeGeometry = new THREE.PlaneGeometry(600, 200, 20, 20);
        var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});
        var plane = new THREE.Mesh(planeGeometry, planeMaterial);
        plane.receiveShadow = true;

        // rotate and position the plane
        plane.position.x = 0;
        plane.position.y = 600;
        plane.position.z = 600;

        // add the plane to the scene
        scene.add(plane);



        // position and point the camera to the center of the scene
        camera.position.x = 0;
        camera.position.y = 300;
        camera.position.z = 500;
        camera.lookAt(new THREE.Vector3(10, 0, 0));

        // add subtle ambient lighting
        var ambiColor = "#1c1c1c";
        var ambientLight = new THREE.AmbientLight(ambiColor);
        scene.add(ambientLight);


        var target = new THREE.Object3D();
        target.position = new THREE.Vector3(5, 0, 0);


        var pointColor = "#ff5808";
        var directionalLight = new THREE.DirectionalLight(pointColor);
        directionalLight.position.set(0, 500, 500);

        directionalLight.castShadow = true;
//        directionalLight.shadowCameraNear = 2;
//        directionalLight.shadowCameraFar = 200;
//        directionalLight.shadowCameraLeft = -50;
//        directionalLight.shadowCameraRight = 50;
//        directionalLight.shadowCameraTop = 50;
//        directionalLight.shadowCameraBottom = -50;

        directionalLight.distance = 1000;
        directionalLight.intensity = 0.5;
//        directionalLight.shadowMapHeight = 1024;
//        directionalLight.shadowMapWidth = 1024;


        scene.add(directionalLight);
        var lightHelper=new THREE.DirectionalLightHelper(directionalLight);
        scene.add(lightHelper);
        // add a small sphere simulating the pointlight


        // add the output of the renderer to the html element
        document.getElementById("WebGL-output").appendChild(renderer.domElement);

        // call the render function
        var step = 0;

        // used to determine the switch point for the light animation
        var invert = 1;
        var phase = 0;




        render();

        function render() {
            stats.update();
            // rotate the cube around its axe


            // render using requestAnimationFrame
            requestAnimationFrame(render);


            renderer.render(scene, camera);
        }

        function initStats() {

            var stats = new Stats();

            stats.setMode(0); // 0: fps, 1: ms

            // Align top-left
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.left = '0px';
            stats.domElement.style.top = '0px';

            document.getElementById("Stats-output").appendChild(stats.domElement);

            return stats;
        }
    }

    window.onload = init


</script>
</body>
</html>